﻿// // ******************************************************************
// //       /\ /|       @file       ConfigDataQueryHandler.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2025-02-01 14:02
// //    *(__\_\        @Copyright  Copyright (c) , 2025-01-29 21:01 Await
// // ******************************************************************

using AutomaticSubtitles.Contracts.Dto;
using AutomaticSubtitles.Service.Application.Config.Commands;
using AutomaticSubtitles.Service.Application.Config.Queries;
using AutomaticSubtitles.Service.Domain.Config;

namespace AutomaticSubtitles.Service.Application.Config;

public class ConfigDataQueryHandler(IConfigDataRepository repository) : BaseAutoService
{
    [EventHandler]
    public async Task GetListConfigQuery(GetListConfigQuery query)
    {
        var list = await repository.GetListAsync();
        query.Result = list.MapToList<ConfigDto>();
    }


    [EventHandler]
    public async Task Handle(GetConfigTokenQuery query)
    {
        var autoConfig = await repository.FindAsync(x => x.Id == query.id);
        if (autoConfig == null) throw new ArgumentException(ReturnMessage(NoExistent));

        if (autoConfig.Token.IsNullOrWhiteSpace())
        {
            var command = new UpdateConfigTokenCommand(query.id);
            await PublishAsync(command);
            // query.Result = command.Result;
            // return;
        }

        query.Result = autoConfig.Token;
    }

    [EventHandler]
    public async Task Handle(GetConfigTypeListData query)
    {
        var data = await repository.GetListAsync(x => x.Type == query.TypeEnum);
        query.Result = data.Select(x => new BasicsSelectDto<string>()
        {
            Value = x.Id,
            Label = x.Name
        }).ToList();
    }
}